package com.fbty.mt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.xmlbeans.impl.jam.mutable.MPackage;

import com.fbty.base.dao.MtJdbcDao;
import com.fbty.base.domain.Page;
import com.fbty.mt.dao.AnnDao;
import com.fbty.mt.dao.NewsDao;
import com.fbty.mt.dao.UserDao;
import com.fbty.mt.domain.Announcement;
import com.fbty.mt.domain.News;
import com.fbty.mt.domain.User;
import com.fbty.mt.service.NewsService;
import com.fbty.mt.service.UserService;
/**
 * w公告服务类
 * @author thinkpad
 *
 */
public class NewsServiceImpl implements NewsService{
	private NewsDao newsDao;
	private MtJdbcDao mtJdbcDao;
	private AnnDao annDao;

	@Override
	public void deleteNews(Integer id) {
		newsDao.removeById(id);
	}

	/**
	 * 分页查出所有的公告
	 * @param pageNo
	 * @param parms
	 * @return
	 */
	public Page pageNews(Integer pageNo,Integer pageSize,Map parms) {
		String sql =  " select *,DATE_FORMAT(createdate,'%Y-%m-%d') as datetime from tab_news tn order by id desc";
		List<Map> list = mtJdbcDao.getJdbcTemplate().queryForList(sql);
		List<Map<String, String>> list2 = new ArrayList<Map<String,String>>();
		for(int i=0; i<list.size(); i++) {
			Map map = new HashMap();
			map.put("id", list.get(i).get("id"));
			map.put("title", list.get(i).get("title"));
			map.put("type", list.get(i).get("type"));
			map.put("createdate", list.get(i).get("datetime"));
			String str = list.get(i).get("context")+"";
			if(str.length() > 40) {
				str = str.substring(0, 40)+"...";
			}
			map.put("text", str);
			map.put("context", list.get(i).get("context"));
			list2.add(map);
		}
		Integer count = list2.size();
		return new Page(list2, pageNo*pageSize, count, pageSize);
	}
	/**
	 * 查找某一个news
	 * @param pageNo
	 * @param parms
	 * @return
	 */
	public News getNewsById(Integer id) {
		return newsDao.get(id);
	}

	@Override
	public void saveNews(News news) {
		newsDao.save(news);
	}

	@Override
	public void updateNew(News news) {
		newsDao.update(news);
		
	}
	/**
	 * 改变消息状态是否发布到前台
	 * @param id 消息id
	 * @throws Exception
	 */
	public void updateNewsState(Integer id) {
		News news = newsDao.get(id);
		if(news.getType() == 1) {
			news.setType(0);
		} else {
			news.setType(1);
		}
		newsDao.update(news);
	}
	
	
	/**
	 * 
	 * ------------------------------------------------------------------
	 */
	
	
	@Override
	public void deleteAnn(Integer id) {
		annDao.removeById(id);
	}
	@Override
	public Announcement getAnnById(Integer id) {
		return annDao.get(id);
	}
	@Override
	public Page pageAnn(Integer pageNo, Integer pageSize, Map parms) {
		String sql =  " select *,DATE_FORMAT(createdate,'%Y-%m-%d') as datetime from tab_ann order by id desc";
		List<Map> list = mtJdbcDao.getJdbcTemplate().queryForList(sql);
		List<Map<String, String>> list2 = new ArrayList<Map<String,String>>();
		for(int i=0; i<list.size(); i++) {
			Map map = new HashMap();
			map.put("id", list.get(i).get("id"));
			map.put("title", list.get(i).get("title"));
			map.put("type", list.get(i).get("type"));
			map.put("createdate", list.get(i).get("datetime"));
			String str = list.get(i).get("context")+"";
			if(str.length() > 40) {
				str = str.substring(0, 40)+"...";
			}
			map.put("text", str);
			map.put("context", list.get(i).get("context"));
			list2.add(map);
		}
		Integer count = list2.size();
		return new Page(list2, pageNo*pageSize, count, pageSize);
	}

	@Override
	public void saveAnn(Announcement Ann) {
		annDao.save(Ann);
	}

	@Override
	public void updateAnn(Announcement Ann) {
		annDao.update(Ann);
	}

	@Override
	public void updateAnnState(Integer id) {
		Announcement ann = annDao.get(id);
		if(ann.getType() == 0) {
			ann.setType(1);
		} else {
			ann.setType(0);
		}
		annDao.update(ann);
	}
	
	
	
	public NewsDao getNewsDao() {
		return newsDao;
	}

	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}

	public MtJdbcDao getMtJdbcDao() {
		return mtJdbcDao;
	}

	public void setMtJdbcDao(MtJdbcDao mtJdbcDao) {
		this.mtJdbcDao = mtJdbcDao;
	}

	public AnnDao getAnnDao() {
		return annDao;
	}

	public void setAnnDao(AnnDao annDao) {
		this.annDao = annDao;
	}

	
	
}
